package com.dragon.modules.yuegou.utils;

import java.math.BigDecimal;

public class Random {
    private static BigDecimal[] weightArrays = {BigDecimal.valueOf(0.00000),BigDecimal.valueOf(25.00000),
            BigDecimal.valueOf(0.00000),BigDecimal.valueOf(25.00000),
            BigDecimal.valueOf(0.00000),BigDecimal.valueOf(25.00000),
            BigDecimal.valueOf(0.00000), BigDecimal.valueOf(25.00000)};  // 数组下标是要返回的值，数组值为数组下标的权重

    public static class WeightRandom {
        private BigDecimal weightArraySum(BigDecimal[] weightArrays) {
            BigDecimal weightSum = BigDecimal.ZERO;
            for (BigDecimal weightValue : weightArrays) {
                weightSum = weightSum.add(weightValue);

            }
            return weightSum;
        }

        public int getWeightRandom(BigDecimal[] weightArrays) {
            BigDecimal weightSum = weightArraySum(weightArrays);
            BigDecimal stepWeightSum = BigDecimal.ZERO;
            double[] stepWeightArg = new double[weightArrays.length];
            for (int i = 0; i < weightArrays.length; i++) {
                stepWeightSum = stepWeightSum.add(weightArrays[i]);
                double stepWeightSumDouble = stepWeightSum.divide(weightSum, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
                if (weightArrays[i].compareTo(BigDecimal.ZERO) != 0) {
                    stepWeightArg[i] = stepWeightSumDouble;
                } else {
                    stepWeightArg[i] = 0;
                }
            }
            for (int i = 0; i < weightArrays.length; i++) {
                double random = Math.random();
                for (int j = 0; j < stepWeightArg.length; j++) {
                    if (random <= stepWeightArg[j]) {
                        return j;
                    }
                }
            }
            return -1;
        }
    }
}